package com.medicine.prm.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medicine.common.base.SerialNumber;
import com.medicine.common.exception.WarnException;
import com.medicine.common.utils.ScrmDateUtil;
import com.medicine.prm.entity.member.Tbsmember;
import com.medicine.prm.entity.patient.Tbspatient;
import com.medicine.prm.mapper.MemberMapper;
import com.medicine.prm.mapper.OutpatientMaaper;
import com.medicine.prm.param.AppointmentParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;

@Service
public class OutpatientService extends ServiceImpl<OutpatientMaaper,Tbspatient> {

    @Autowired
    private MemberMapper memberMapper;


    public Tbspatient getTbspatient(AppointmentParam appointmentParam) throws WarnException, ParseException {

        Integer sex = appointmentParam.getIsex()!=null?appointmentParam.getIsex():1;
        if(sex==0){sex=1;};
        Integer age = StringUtils.isNotBlank(appointmentParam.getIage())?Integer.valueOf(appointmentParam.getIage()):0;
        Integer month = StringUtils.isNotBlank(appointmentParam.getImonth())?Integer.valueOf(appointmentParam.getImonth()):0;

        Tbsmember tbsmember = memberMapper.findByCriteria(appointmentParam.getShospitalid(),appointmentParam.getSorgid(),null,appointmentParam.getSmobile());
        if(null == tbsmember){
            tbsmember = new Tbsmember();
            tbsmember.setSorgid(appointmentParam.getSorgid());
            tbsmember.setShospitalid(appointmentParam.getShospitalid());
            tbsmember.setSmobile(appointmentParam.getSmobile());
            tbsmember.setSname(appointmentParam.getSname());
            tbsmember.setDregistertime(new Date());
            tbsmember.setDcreatetime(new Date());
            tbsmember.setScard(appointmentParam.getScard());
            memberMapper.insert(tbsmember);
        }
        String siptotpno = SerialNumber.getNum("");
        String smdtrtcertno = SerialNumber.getSixRandNum("ZH");
        if(StringUtils.isNotBlank(appointmentParam.getScard())){
            // findTbspatientByCertno(appointmentParam.getShospitalid(),appointmentParam.getScard());
            Tbspatient   t =  baseMapper.selectOne(new QueryWrapper<Tbspatient>().lambda().eq(Tbspatient::getShospitalid,appointmentParam.getShospitalid()).eq(Tbspatient::getScard ,appointmentParam.getScard()));
            if(t!=null){
                siptotpno = t.getSiptotpno();
                smdtrtcertno = t.getSmdtrtcertno();
            }
        }
        //Tbspatient  patient = findPatientBySnameAndSmobile(sname,smobile,tbsmember.getSmemberid());
        Tbspatient  patient = baseMapper.selectOne(new QueryWrapper<Tbspatient>().lambda().eq(Tbspatient::getSname,appointmentParam.getSname()).eq(Tbspatient::getSmobile,appointmentParam.getSmobile())
        .eq(Tbspatient::getSmemberid,tbsmember.getSmemberid()));
        if(patient!=null){
            patient.setDcratetime(new Date());
            patient.setDupdatetime(new Date());
            patient.setIsdelete(1);
            patient.setIstatus(1);
            patient.setIsex(appointmentParam.getIsex());
            patient.setSname(appointmentParam.getSname());
            patient.setSmobile(appointmentParam.getSmobile());
            patient.setSmemberid(tbsmember.getSmemberid());
            patient.setScard(appointmentParam.getScard());
            patient.setSmdtrtcertno(smdtrtcertno);
            patient.setSiptotpno(siptotpno);
            if(StringUtils.isBlank(appointmentParam.getDbirthday())){
                patient.setDbirth(ScrmDateUtil.getDbrithByAge(age,month));
            }else{
                patient.setDbirth(ScrmDateUtil.parseToDate(appointmentParam.getDbirthday(), ScrmDateUtil.yyyyMMdd));
            }
            save(patient);
        }else{
            patient = new Tbspatient();
            patient.setDcratetime(new Date());
            patient.setDupdatetime(new Date());
            patient.setIsdelete(1);
            patient.setIstatus(1);
            patient.setIsex(sex);
            patient.setSname(appointmentParam.getSname());
            patient.setSmobile(appointmentParam.getSmobile());
            patient.setSmemberid(tbsmember.getSmemberid());
            patient.setScard(appointmentParam.getScard());
            patient.setSiptotpno(siptotpno);
            patient.setSmdtrtcertno(smdtrtcertno);
            patient.setIshow(2);
            patient.setIsource(1);
            patient.setIsdefault(2);
            if(StringUtils.isBlank(appointmentParam.getDbirthday())){
                patient.setDbirth(ScrmDateUtil.getDbrithByAge(age,month));
            }else{
                patient.setDbirth(ScrmDateUtil.parseToDate(appointmentParam.getDbirthday(), ScrmDateUtil.yyyyMMdd));
            }
            updateById(patient);
        }
        return patient;
    }
}
